<template>
	<jk-dialog
		v-model="showDialog"
		:title="`选择-设备`"
		width="900px"
		append-to-body
		@on-visible-change="onVisibleChange"
		@on-cancel="onVisibleChange(false)"
		@on-confirm="onConfirmBtn"
	>
		<header ref="box1" class="margin-bottom-4 flex-end-center">
			<jk-select v-model="searchParams.workshopId" :option-list="workshopList" clearable placeholder="区域" size="small" class="search-bar-item-size2 item-gap"></jk-select>
			<jk-select v-model="searchParams.processId" :option-list="processList" placeholder="工序" clearable size="small" class="search-bar-item-size1 item-gap"></jk-select>
			<jk-select v-model="searchParams.typeCode" value-key-name="code" :option-list="machineTypeList" clearable size="small" placeholder="设备类型" class="search-bar-item-size1 item-gap"></jk-select>
			<jk-search-input v-model="searchParams.nameOrCode" class="search-bar-item-size2 item-gap" placeholder="设备编号或名称" @on-search="onSearchBtn"></jk-search-input>
			<jk-search-button @click="onSearchBtn"></jk-search-button>
		</header>

		<jk-table
			ref="jkTable"
			:loading="tableLoading"
			class="width-100-percent"
			:data="tableData"
			border
			highlight-hover-row
			:height="dialogTableHeight - 30"
			:checkbox-config="{checkMethod: checkboxMethod}"
			@checkbox-change="selectChangeEvent"
		>
			<vxe-column type="checkbox" width="60" align="center"></vxe-column>
			<vxe-column title="设备编号" align="left" field="code" :min-width="90" />
			<vxe-column title="设备名称" align="left" field="name" :min-width="90" />
			<vxe-column title="机型" align="left" field="machineModelName" :min-width="90" />
			<vxe-column title="设备类型" align="left" field="typeName" :min-width="90" />
			<vxe-column title="区域" align="left" field="workshopName" :width="90" />
			<vxe-column title="工序" align="center" field="processName" :width="60" />
		</jk-table>
		<footer ref="box2" class="flex-end-center margin-top-10">
			<jk-page
				:page-index="searchParams.pageNo"
				:page-sizes="searchParams.pageSizes"
				:page-size="searchParams.pageSize"
				:total="totalCount"
				@on-change="onPageChange"
			/>
		</footer>
	</jk-dialog>
</template>

<script>
    import calDialogHeight from '../../../../mixins/cal-dialog-height';
    import { pageConfig } from '../../../../utils/jk/jk-tool';
    import { machinemanagerMachinePage } from '../../../../api/machine/machineArchives';
    import { systemBusinessDictPage } from '../../../../api/basicData/businessDict';

    export default {
        mixins: [calDialogHeight],
        props: {
            dialogState: {
                type: Boolean,
                default: false
            },
            defaultData: {
                type: Object,
                default() {
                    return {};
                }
            },
            checkKeyName: {
                type: String,
                default: 'machineId'
            },
            defaultProcessName: {
                type: String,
                default: ''
            },
            checkedData: {
                type: Array,
                default() {
                    return [];
                }
            }
        },
        data() {
            return {
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    processId: '',
                    workshopId: null,
                    name: null,
                    nameOrCode: '',
                    typeCode: '',
                    auditState: 1
                },
                machineTypeList: [],
                totalCount: 0,
                workshopList: [],
                processList: [],
                checkArr: [],
                selectRow: null,
                tableData: [],
                showDialog: false,
                tableLoading: false,
                selectData: []
            };
        },
        methods: {
            selectChangeEvent({ records }) {
                this.checkArr = records.filter(x => !x._disabled).map(x => {
                    return x;
                });
            },
            checkboxMethod(e) {
                return !e.row._disabled;
            },
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            onConfirmBtn() {
                if (this.checkArr.length) {
                    const arr = this.checkArr.filter(x => !x._disabled);
                    this.$emit('on-confirm', arr);
                } else {
                    this.$tipsBar(this, 'custom', 'warning', '请选择数据');
                }
            },
            setCheckState(arr) {
                arr.forEach((x) => {
                    x._disabled = false;
                    this.checkedData.forEach(k => {
                        if (x.id === k[this.checkKeyName]) {
                            x._disabled = true;
                        }
                    });
                });
                this.tableData = arr;
                this.tableData.forEach((x, index) => {
                    if (x._disabled) {
                        this.$refs.jkTable.$refs.xTable.setCheckboxRow([this.tableData[index], this.tableData[index]], true);
                    }
                });
            },
            getListRequest() {
                this.tableLoading = true;
                this.selectData = [];
                const params = JSON.parse(JSON.stringify(this.searchParams));
                delete params.processCode;
                return machinemanagerMachinePage(params).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.totalCount = res.data.count;
                        this.setCheckState(res.data.res || []);
                    }
                });
            },
            onVisibleChange(e) {
                this.$emit('on-visible-change', e);
                if (!e) {
                    Object.assign(this.$data, this.$options.data());
                }
            },
            getTypeListRequest() {
                return systemBusinessDictPage({ categoryCode: 'MACHINE_TYPE' }).then(res => {
                    if (res.data.status === 200) {
                        this.machineTypeList = res.data.res;
                    }
                });
            },
            async getDependentData() {
                this.tableLoading = true;
                this.workshopList = this.$store.state.user.userWorkshop.workshopList;
                this.processList = this.$store.state.user.userProcess.processList;
                if (this.defaultData.workshopId) {
                    this.$set(this.searchParams, 'workshopId', this.defaultData.workshopId);
                } else {
                    this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
                }
                if (this.defaultData.processId) {
                    this.$set(this.searchParams, 'processId', this.defaultData.processId);
                } else {
                    this.$set(this.searchParams, 'processId', this.$store.state.user.userProcess.defaultProcessId);
                }
                await this.getTypeListRequest();
                await this.getListRequest();
            }
        },
        watch: {
            dialogState(newVal) {
                this.showDialog = newVal;
                if (newVal) {
                    this.getDependentData();
                }
            }
        }
    };
</script>
